import { ref } from "vue";
import { defineStore } from "pinia";

export const usePageStore = defineStore(
    "page",
    () => {
        const currentPage = ref(localStorage.getItem("currentPage") || 1);
        const updateCurrentPage = (val) => {
            currentPage.value = val;
            localStorage.setItem("currentPage", val);
        }
        const removeCurrentPage = () => {
            currentPage.value = 1;
            localStorage.removeItem("currentPage");
        }
        return { currentPage, updateCurrentPage, removeCurrentPage }

        // 监听页面卸载事件，重置 currentPage
        onUnmounted(() => {
            removeCurrentPage();
        });

    }, {
        persist: true,
    }

);